package com.yangchao.ch03;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** java 正则表达式写法 **/
public class example12 {
    public static void main(String[] args) {


        String targetStr = "895687";  //目标字符串
        String regex = "(\\d)(\\d)(\\d)(\\d)"; //正则表达式字符串

        //完全匹配

        boolean isMatch = Pattern.matches(regex, targetStr);
        System.out.println("完整匹配：|" + isMatch);

        //捕获组

        Pattern p =Pattern.compile(regex);
        Matcher m = p.matcher(targetStr);
        if (m.find()){
            int groupCount = m.groupCount();
            for (int i =0; i <= groupCount;i++){
                System.out.println("Group"+i+":"+m.group(i));
            }
        }
    }
}
